package gang.com.twoshopping;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import gang.com.twoshopping.bean.Tab;
import gang.com.twoshopping.fragment.CartFragment;
import gang.com.twoshopping.fragment.CategoryFragment;
import gang.com.twoshopping.fragment.HomeFragment;
import gang.com.twoshopping.fragment.HotFragment;
import gang.com.twoshopping.fragment.MineFragment;
import gang.com.twoshopping.weight.FragmentTabHost;

public class MainActivity extends AppCompatActivity {
    private FragmentTabHost mTabhost;
    private LayoutInflater mInflater;
    private List<Tab> mtabList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //封装的重要性
        initTab();
    }

    private void initTab() {
        Tab tab_home = new Tab(R.string.home, HomeFragment.class, R.drawable.selector_icon_home);
        Tab tab_hot = new Tab(R.string.hot, HotFragment.class, R.drawable.selector_icon_hot);
        Tab tab_cart = new Tab(R.string.cart, CartFragment.class, R.drawable.selector_icon_cart);
        Tab tab_category = new Tab(R.string.catagory, CategoryFragment.class, R.drawable.selector_icon_category);
        Tab tab_mine = new Tab(R.string.mine, MineFragment.class, R.drawable.selector_icon_mine);
        mtabList.add(tab_home);
        mtabList.add(tab_hot);
        mtabList.add(tab_cart);
        mtabList.add(tab_category);
        mtabList.add(tab_mine);

        mInflater = LayoutInflater.from(this);
        mTabhost = (FragmentTabHost) this.findViewById(android.R.id.tabhost);
        //狸猫换太子
        mTabhost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        for (Tab tab : mtabList) {
            TabHost.TabSpec tabSpec = mTabhost.newTabSpec(getString(tab.getTitle()));
            //TabSpec需要一个Indicator
            tabSpec.setIndicator(buileIndicator(tab));
            mTabhost.addTab(tabSpec, tab.getFragment(), null);
        }
        //去掉分割线
        mTabhost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
        mTabhost.setCurrentTab(0);
    }

    private View buileIndicator(Tab tab) {
        View view = mInflater.inflate(R.layout.tab_indcator, null);
        ImageView img = (ImageView) view.findViewById(R.id.icon_tab);
        TextView text = (TextView) view.findViewById(R.id.txt_indicator);
        img.setBackgroundResource(tab.getIcon());
        text.setText(tab.getTitle());
        return view;
    }

}
